//  CNDiscountCouponController.m
//  BeeQuick_19
//
//  Created by clinging on 16/9/26.
//  Copyright © 2016年 CN Team. All rights reserved.
//

#import "CNDiscountCouponController.h"
#import "Masonry.h"
#import "CNDiscountCouponCell.h"
#import "CNDiscountModel.h"
#import "DSHTTPClient.h"
#import "CNDwebViewController.h"

static NSString *cellID = @"cell";
@interface CNDiscountCouponController ()<UITableViewDataSource,UITableViewDelegate>

@property(weak,nonatomic)UITextField *couponTF;

@property(weak,nonatomic)UITableView *discountCoupenTB;
@end

@implementation CNDiscountCouponController
{
 NSArray <CNDiscountModel *>*_couponList;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.edgesForExtendedLayout = UIRectEdgeNone;
    [self loadData];
    [self setUpUI];
}

- (void)loadData{
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    
    [param setValue:@"9" forKey:@"call"];
    
    
    [DSHTTPClient postUrlString:@"MyCoupon.json.php" withParam:param withSuccessBlock:^(id data) {
        NSMutableArray *tempArr = [[NSMutableArray alloc]init];
        NSArray *modelArr = [data valueForKey:@"data"];
        [modelArr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        CNDiscountModel *model = [[CNDiscountModel alloc]init];
            model = [CNDiscountModel initWithDictionary:obj];
            [tempArr addObject:model];
        }];
        _couponList = tempArr.copy;
        
        //很重要!!!
        [self.discountCoupenTB reloadData];
        
    } withFailedBlock:^(NSError *error) {
        NSLog(@"%@",error);
    } withErrorBlock:^(NSString *message) {
        NSLog(@"%@",message);
    }];
    
}

- (void)setUpUI{
    //navigation相关
    self.navigationItem.title = @"优惠券";
    UIBarButtonItem *RButton = [[UIBarButtonItem alloc]initWithTitle:@"使用规则" style:UIBarButtonItemStylePlain target:self action:@selector(rBtuClick)];
//    RButton.title = @"使用规则";
    
    RButton.tintColor = [UIColor colorWithRed:111/255.0 green:111/255.0 blue:111/255.0 alpha:1];
    self.navigationItem.rightBarButtonItem = RButton;
    
    
    
    UIView *TopView = [[UIView alloc]init];
    TopView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:TopView];
    [TopView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.view);
        make.height.equalTo(@40);
    }];
    
    UITextField *textFD = [[UITextField alloc]init];
    //    textFD.layer.cornerRadius = 5;
    textFD.borderStyle = UITextBorderStyleRoundedRect;
    textFD.font = [UIFont systemFontOfSize:12];
    //    textFD.backgroundColor = [UIColor lightGrayColor];
    textFD.placeholder = @"请输入优惠券号码";
    [TopView addSubview:textFD];
    [textFD mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(TopView).offset(10);
        make.top.equalTo(TopView).offset(8);
        make.right.equalTo(TopView).offset(-80);
        make.bottom.equalTo(TopView).offset(-8);
        //        make.centerY.equalTo(TopView);
    }];
    _couponTF = textFD;
    
    UIButton *lockBtn = [[UIButton alloc]init];
    lockBtn.layer.cornerRadius = 5;
    lockBtn.font = [UIFont systemFontOfSize:12];
    [TopView addSubview:lockBtn];
    [lockBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        //        make.centerY.equalTo(TopView);
        make.top.bottom.equalTo(textFD);
        make.left.equalTo(textFD.mas_right).offset(8);
        make.right.equalTo(TopView).offset(-10);
    }];
    lockBtn.backgroundColor = [UIColor colorWithRed:255/255.0 green:216/255.0 blue:0 alpha:1];
    [lockBtn setTitle:@"绑定" forState:UIControlStateNormal];
    [lockBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [lockBtn addTarget:self action:@selector(lockBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    UITableView *discountCoupenTB = [[UITableView alloc]init];
    discountCoupenTB.delegate = self;
    discountCoupenTB.dataSource = self;
    discountCoupenTB.estimatedRowHeight = 100;
    discountCoupenTB.rowHeight = UITableViewAutomaticDimension;
    discountCoupenTB.separatorStyle = UITableViewCellSeparatorStyleNone;
    [discountCoupenTB registerClass:[CNDiscountCouponCell class] forCellReuseIdentifier:cellID];
    [self.view addSubview:discountCoupenTB];
    [discountCoupenTB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(TopView.mas_bottom);
        make.left.bottom.right.equalTo(self.view);
    }];
    _discountCoupenTB = discountCoupenTB;
    
    
}
-(void)rBtuClick{

 CNDwebViewController * webVC = [[CNDwebViewController alloc]init];
//    
//    [self presentViewController:webVC animated:YES completion:nil];
    [self.navigationController pushViewController:webVC animated:YES];
    NSLog(@"我被点击了");
//    

}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _couponList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    CNDiscountCouponCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath];
    cell.start_timeLB.text = _couponList[indexPath.row].start_time;
    cell.end_timeLB.text = _couponList[indexPath.row].end_time;
    cell.wordLB.text = _couponList[indexPath.row].name_us;
    cell.valueLB.text = _couponList[indexPath.row].value;
    cell.descLB.text = _couponList[indexPath.row].desc;
    if (_couponList[indexPath.row].is_userd) {
        cell.backgroundImg.image = [UIImage imageNamed:@"v2_coupon_gray"];
        cell.usedImg.hidden = NO;
        cell.roundImg.image = [UIImage imageNamed:@"grey1"];
    }else{
        cell.usedImg.hidden = YES;
    }
    
    return cell;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    [_couponTF resignFirstResponder];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [_couponTF resignFirstResponder];
}

- (void)lockBtnClick:(UIButton *)sender{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"请输入正确的优惠券号码" preferredStyle:UIAlertControllerStyleAlert];
    [alert addAction:[UIAlertAction actionWithTitle:@"去输入" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        [_couponTF becomeFirstResponder];
    }]];
    [alert addAction:[UIAlertAction actionWithTitle:@"离开" style: UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        [_couponTF resignFirstResponder];
    }]];
    [self presentViewController:alert animated:NO completion:nil];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
